/**
 * @class Bible.grid.BookThemesGridPanel
 * @extends Ext.grid.EditorGridPanel
 */
(function(){
    var BookThemesGridPanel,
        BOOKS_URL = 'servlets/books.php';

    BookThemesGridPanel = Ext.extend(Ext.grid.EditorGridPanel, {
        cls: 'bible-themes-grid',
		stripeRows: true,
        columnLines: true,
        clicksToEdit: 1,

		initComponent: function(){
            var me = this;

            // TODO not on all browsers looks same
            me.textFieldEditor = new Ext.form.TextField({
                maxLength: 255,
//                style: 'padding: 2px 6px 3px 6px'
                style: 'padding: 3px 6px'
            });
            me.themeGridEditor = new Ext.grid.GridEditor(me.textFieldEditor, {
                offsets: [-2, -1]
            });

            me.store = new Ext.data.ArrayStore({
				//autoLoad: true,
                proxy: new Ext.data.HttpProxy(new Ext.data.Connection({
                    method: 'GET',
					url: BOOKS_URL,
					autoAbort: true
				})),
                baseParams: {
                    action: 'get_themes',
                    book: 1 // TODO temporary to load first book
                },
                root: 'data',
                idIndex: 0,
                fields: [
                    {name: 'cnumber', type: 'int'},
                    {name: 'theme', type: 'string'}
                ]
            });

            me.booksCombo = new Bible.form.BibleBooksComboBox({
//                value: me.defaultBook,
				infoTooltip: false,
                value: 1
            });

			Ext.apply(me, {
				viewConfig: {
					autoFill: true,
                    scrollOffset: 25,
					forceFit: true
				},
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true
				}),
				tbar: new Ext.Toolbar({
                    items: [
                        {
                            xtype: 'spacer',
                            cls: 'toolbar-icon'
                        },
                        me.booksCombo
                    ]
                }),
                cm: new Ext.grid.ColumnModel({
                    columns: [
                        new Ext.grid.RowNumberer({
                            dataIndex: 'cnumber',
                            width: 27
                        }),
                        {
                            id: 'book-themes',
                            hideable: false,
                            menuDisabled: true,
                            header: i18n.chapterTheme,
                            dataIndex: 'theme',
                            editor: me.themeGridEditor
                        },
                        {
                            id: 'book-action',
                            dataIndex: 'id',
                            width: 27,
                            fixed: true,
                            hideable: false,
                            menuDisabled: true,
                            renderer: function(v, metaData){
                                return '';
                            }
                        }
                    ]
                })
			});

			BookThemesGridPanel.superclass.initComponent.call(me);
		},

		initEvents: function(){
            var me = this,
                store = me.store;
			BookThemesGridPanel.superclass.initEvents.apply(me, arguments);

            me.booksComboChange(me.booksCombo.store.getAt(0));

            me.on({
                scope: me,
                afteredit: function(e){
                    var book = me.selectedBook;
                    Bible.updateDBObj('edit_list', e, BOOKS_URL + "?book=" + book, function(){
                        Bible.EventManager.fireEvent('themechange', me, book, e.record.id, e.value);
                    });
                }
            });

            me.mon(Bible.EventManager, {
                themechange: function(grid, book, chapter, newTheme){
                    console.info('themechange', book, chapter, newTheme);
                    if(me != grid && me.selectedBook == book){
                        // TODO temporary
                        me.store.reload();
                    }
                }
            });

            me.textFieldEditor.on('show', function() {
                var h = me.getView().getCell(store.indexOf(me.getSelectionModel().getSelected()), 2).clientHeight;
                this.setSize(this.getWidth() + 3, h);
            });

            me.booksCombo.on({
                select: function(combo, record, index){
                    me.booksComboChange(record);
                }
            });
		},

        booksComboChange: function(record){
            console.debug('booksComboChange', record.data);
            var book = record.get('id');
            this.selectedBook = book;
            this.store.load({
                params: {
                    book: book
                }
            });
        }
    });

    Bible.grid.BookThemesGridPanel = BookThemesGridPanel;

    Ext.reg('bookthemesgridpanel', BookThemesGridPanel);

})();
